package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class SetupJALibrary extends JDialog {

	private static final long serialVersionUID = 1L;

	private JLabel labelMessage;
	private JTextField textFieldName;
	private JTextField textFieldPassword;
	private JTextField textFieldDatabase;
	private JTextField textFieldLocalhost;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SetupJALibrary dialog = new SetupJALibrary();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SetupJALibrary() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(
				new MigLayout("", "[][grow]", "[][][][][][][][][][][]"));
		{
			JLabel lblWelocomeToJa = new JLabel("Welcome to J&A Library Setup!");
			lblWelocomeToJa.setFont(new Font("Arial", Font.BOLD, 16));
			getContentPane().add(lblWelocomeToJa, "cell 0 0 2 1,alignx center");
		}
		{
			JLabel label = new JLabel("     ");
			getContentPane().add(label, "cell 0 1");
		}
		{
			JLabel lblPleaseFillIn = new JLabel(
					"Please fill in the fields below with the correct information.");
			getContentPane().add(lblPleaseFillIn, "cell 0 2 2 1");
		}
		{
			JButton btnNewButton = new JButton("Enter");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					//
					if (createConfigFile()) {
						dispose();
					}
				}
			});
			{
				JLabel lblSqlMachine = new JLabel("SQL machine:");
				getContentPane().add(lblSqlMachine, "cell 0 4,alignx trailing");
			}
			{
				textFieldLocalhost = new JTextField();
				try {
					textFieldLocalhost.setText(InetAddress.getLocalHost()
							.getLocalHost().getHostAddress());
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				getContentPane().add(textFieldLocalhost, "cell 1 4,growx");
				textFieldLocalhost.setColumns(10);
			}
			{
				JLabel lblNewLabel = new JLabel("SQL username:");
				getContentPane().add(lblNewLabel, "cell 0 5,alignx trailing");
			}
			{
				textFieldName = new JTextField();
				textFieldName.setText("libtester");
				getContentPane().add(textFieldName, "cell 1 5,growx");
				textFieldName.setColumns(10);
			}
			{
				JLabel lblSqlPassword = new JLabel("SQL password:");
				getContentPane()
						.add(lblSqlPassword, "cell 0 6,alignx trailing");
			}
			{
				textFieldPassword = new JTextField();
				textFieldPassword.setText("secret");
				getContentPane().add(textFieldPassword, "cell 1 6,growx");
				textFieldPassword.setColumns(10);
			}
			{
				JLabel lblDatabaseName = new JLabel("Database Name:");
				getContentPane().add(lblDatabaseName,
						"cell 0 7,alignx trailing");
			}
			{
				textFieldDatabase = new JTextField();
				textFieldDatabase.setText("librarydb");
				getContentPane().add(textFieldDatabase, "cell 1 7,growx");
				textFieldDatabase.setColumns(10);
			}
			getContentPane().add(btnNewButton, "cell 0 9,growx");
		}
		{
			labelMessage = new JLabel("     ");
			labelMessage.setForeground(Color.BLUE);
			getContentPane().add(labelMessage, "cell 0 10 2 1");
		}
	}

	private boolean createConfigFile() {
		if (textFieldName.getText().trim().equals("")) {
			labelMessage.setText("SQL machine must not be empty.");
			return false;
		}
		if (textFieldName.getText().trim().equals("")) {
			labelMessage.setText("SQL username must not be empty.");
			return false;
		}
		if (textFieldDatabase.getText().trim().equals("")) {
			labelMessage.setText("Database name must not be empty.");
			return false;
		}
		DBConnector.writeConfig(textFieldLocalhost.getText(),
				textFieldName.getText(), textFieldPassword.getText(),
				textFieldDatabase.getText());
		try {
			DBConnector.writeSQLScript();
			DBConnector.writeCreateDBBatchfile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
		return true;
	}

}
